<template>
    <div>
        <Bar>
            <template v-slot:title>
               设置密码
            </template>
        </Bar>
          <van-form>
  <van-field
  v-model="mobile"
  center
  clearable
  placeholder="请输入手机号"
>
  <template #button>
    <van-button size="small" type="default" style="color:orange" @click="btnSms">发送验证码</van-button>
  </template>
</van-field>
  <van-field
    v-model="sms"
    type="password"
    placeholder="验证码"
  >
  <template #button>
   <span v-show="show">({{num}})</span>
  </template>
  </van-field>
   <van-field
  v-model="password"
  center
  clearable
  placeholder="请输入密码"
>
</van-field>
 <div style="margin:.6rem;">
    <van-button round block  color="linear-gradient(to right, rgb(246,160,113), rgb(249,88,2))" native-type="submit" @click="login">确定</van-button>
  </div>
          </van-form>
    </div>
</template>

<script>
import { Toast } from "vant";
import Bar from '../Bar'
import {smsode,passwords} from "../../Request/http.js";
export default {
    components:{Bar},
    name: 'EverydaySetnewpass',

    data() {
        return {
           num: 6,
      show: false,
      sms: "",
      mobile: "",
      password: ""
        };
    },

    mounted() {
        
    },

    methods: {
         btnSms() {
      var smss = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (!smss.test(this.mobile)) {
        Toast.fail("手机号格式不正确");
        this.mobile = "";
        return false;
      }
      smsode({ sms_type: "login", mobile: this.mobile }).then(res => {
        console.log(res, "sms");
        if (res) {
          this.show = true;
          var timer = setInterval(() => {
            this.num--;
            if (this.num == 0) {
              clearInterval(timer);
              this.show = false;
              this.num = 6;
            }
          }, 1000);
        }
      });
       },
    login() {
      passwords({
        mobile: this.mobile,
        sms_code: this.sms,
        password:this.password
      }).then(res => {
        console.log(res, "login");
        this.$router.push("/main");
        localStorage.setItem("login", JSON.stringify(res));
      });
    }
    },
};
</script>

<style lang="scss" scoped>

</style>